package com.lxw.library.views;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.lxw.library.R;
import com.lxw.library.bean.SpinnerInfo;
import com.lxw.library.dialogs.BottomListDialog;

import java.util.ArrayList;
import java.util.List;


/**
 * 作者：李学伟
 * 日期：2018/8/9
 * 用途：
 */

public class LxwSpinner extends LxwBaseView {
    List<SpinnerInfo> spinnerInfoList = new ArrayList<>();
    SpinnerInfo selectItem;

    public LxwSpinner(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showBottomDialog();
            }
        });
    }

    public LxwSpinner(Context context, int orientation) {
        super(context, orientation);
    }

    public LxwSpinner(Context context, int orientation, ColumnRequestListener listener) {
        super(context, orientation, listener);
    }

    public LxwSpinner(Context context, ColumnRequestListener listener) {
        super(context, 0, listener);
    }

    @Override
    public int getHorizontalView() {
        return R.layout.myspinner_h;
    }

    @Override
    public int getVerticalView() {
        return R.layout.myspinner_v;
    }

    @Override
    public SpinnerInfo getValue() {
        return selectItem;
    }


    public void showBottomDialog() {
        if (spinnerInfoList.size() == 0) {
        } else {
            BottomListDialog bottomListDialog = new BottomListDialog(mContext, new BottomListDialog.BottomListSelectListener() {
                @Override
                public void itemClickCallback(int position, SpinnerInfo spinnerInfo) {
                    selectItem = spinnerInfo;
                }
            });
            bottomListDialog.disPlay(spinnerInfoList);
        }
    }

    public void reLoad(List<SpinnerInfo> values, boolean showDialog) {
        this.spinnerInfoList.clear();
        submitCode = "";
        if (null != values) {
            selectItem = values.get(0);
            spinnerInfoList.addAll(values);
        }
        if (showDialog) {
            showBottomDialog();
        }
    }

    @Override
    public boolean checkValueIsLegal() {
        return isMust && selectItem != null;
    }

}
